package com.aplink.generic.widgets;

import java.util.ArrayList;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.ConfigAdapter;
import com.aplink.generic.cache.ContentDataManager;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.ColumnSetting;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.AutoCompleteTextViewSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.Language;
import com.aplink.generic.widgets.custom.FloatingAutoComplete;
import com.aplink.generic.xmlparser.ColumnSettingParser;

public class ApLinkFloatingAutoComplete extends ApLinkBaseView implements
		OnFocusChangeListener {
	private ArrayList<GenericObject> genericObjects;
	private final FloatingAutoComplete mAutoCompleteTextView;
	private final AutoCompleteTextViewSpec mAutoCompleteTextViewSpec;
	private final ArrayList<ColumnSetting> mColumnSetting;
	private ConfigAdapter mConfigAdapter;

	private final ContentData mContentData;

	private GenericDictionary<String, Integer> mIDs;

	public ApLinkFloatingAutoComplete(final GenericFragment genericFragment,
			final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		mAutoCompleteTextView = (FloatingAutoComplete) view;
		mAutoCompleteTextViewSpec = (AutoCompleteTextViewSpec) controlSpec;
		mContentData = ContentDataManager.getInstance().get(
				this.mAutoCompleteTextViewSpec.getContentDataID());
		mColumnSetting = ColumnSettingParser.parse(getGenericFragment(),
				this.mAutoCompleteTextViewSpec.getRptDesignID());
	}

	@Override
	public void clear() {
		mAutoCompleteTextView.getText().clear();
	}

	@Override
	public GenericAttribute getAttribute() {
		final ObjPropInfo primaryKey = this.mContentData.getPrimaryKeys()
				.get(0);
		final String columnNameContentData = primaryKey.getFullColumnName();
		final ObjPropInfo objPropInfo = this.mContentData
				.get(columnNameContentData);
		Integer pos = mIDs.get(mAutoCompleteTextView.getText().toString());
		if (pos != null) {
			final int selectedPosition = mIDs.get(mAutoCompleteTextView
					.getText().toString());
			final GenericObject genericObject = this.genericObjects
					.get(selectedPosition);
			return GenericAttribute.obtain(
					genericObject.getValue(columnNameContentData), objPropInfo);
		}
		return GenericAttribute.obtain(null, objPropInfo);
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		return null;
	}

	@Override
	public FloatingAutoComplete getView() {
		return mAutoCompleteTextView;
	}

	@Override
	protected void initViewProperties() {
		mIDs = new GenericDictionary<>();
		genericObjects = getDatabaseGet().getList(mContentData, null);
		final String fullColumnName = this.mContentData.getMainTable() + "."
				+ this.mAutoCompleteTextViewSpec.getDisplayProperty();
		mConfigAdapter = new ConfigAdapter(getGenericFragment(),
				genericObjects, mColumnSetting,
				mAutoCompleteTextViewSpec.getRowItemID());
		mConfigAdapter.setValueField(fullColumnName);
		mConfigAdapter.setSearchFileds(mAutoCompleteTextViewSpec
				.getSearchFields());
		mAutoCompleteTextView.setAdapter(mConfigAdapter);
		for (int i = 0; i < genericObjects.size(); i++) {
			mIDs.put(genericObjects.get(i).getValue(fullColumnName), i);
		}
	}

	@Override
	public void languageChanged() {
		if (!TextUtils.isEmpty(mAutoCompleteTextView.getHint())) {
			mAutoCompleteTextView.setHint(Language.getInstance().get(
					mAutoCompleteTextView.getHint()));
		}
	}

	@Override
	protected void loadBindingObject() {
		final String dataObj = this.mAutoCompleteTextViewSpec.getDataObject();
		String content = null;
		final String mappingName = this.mAutoCompleteTextViewSpec
				.getMappingName();
		final String fullColumnName = dataObj + "." + mappingName;
		final GenericObject initValue = getGenericFragment().getInitValues();
		if ((initValue != null) && initValue.containsKey(fullColumnName)) {
			content = initValue.get(fullColumnName).getValue();
		} else {
			content = getPassedObject().getValue(fullColumnName);
		}
		if (!TextUtils.isEmpty(content)) {
			final String selectedColumn = String.format("%s.%s",
					this.genericObjects.get(0).getTableName(),
					this.mAutoCompleteTextViewSpec.getMappingName());
			for (int i = 0; i < this.genericObjects.size(); i++) {
				final GenericObject genericObject = this.genericObjects.get(i);
				final String value = genericObject.getValue(selectedColumn)
						.toString();
				if (value.equalsIgnoreCase(content)) {
					String displayColumn = this.mContentData.getMainTable()
							+ "."
							+ this.mAutoCompleteTextViewSpec
									.getDisplayProperty();
					this.mAutoCompleteTextView.setText(genericObject
							.getValue(displayColumn));
					break;
				}
			}
		}
	}

	@Override
	protected void loadDefaultObject() {

	}

	@Override
	public void onFocusChange(final View v, final boolean hasFocus) {
		if (hasFocus) {
			if (mAutoCompleteTextViewSpec.isTouchToShowDropDown()) {
				mAutoCompleteTextView.showDropDown();
			}
		} else {
			if (mAutoCompleteTextView.getText().length() != 0) {
				if (mIDs.get(mAutoCompleteTextView.getText().toString()) == null) {
					mAutoCompleteTextView
							.setError("The value:"
									+ mAutoCompleteTextView.getText()
									+ " is not valid");
					mAutoCompleteTextView.getText().clear();
				}
			}
		}
	}

	@Override
	protected void setEventListener() {
		mAutoCompleteTextView.setOnFocusChangeListener(this);
		mAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(final AdapterView<?> parent,
					final View view, final int position, final long id) {
				mAutoCompleteTextView.setTag(null);
			}
		});
	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {

	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {

	}

}
